package com.homework.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.UserBean;
import com.homework.bean.UserCourseListBean;
import com.homework.service.CourseService;
import com.homework.util.Constants;

public class ClassChooseAction {

	private CourseService courseService;
	private int intPage;
	private String courseName;
	private List<Integer> courseIdList = new ArrayList<Integer>();

	public ClassChooseAction() {
		courseService = new CourseService();
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public List<Integer> getCourseIdList() {
		return courseIdList;
	}

	public void setCourseIdList(List<Integer> courseIdList) {
		this.courseIdList = courseIdList;
	}

	// 选课列表
	public String classChooseShow() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// pageInfo
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());

		// UserCourseListBean
		UserCourseListBean userCourseListBean = new UserCourseListBean();
		userCourseListBean = courseService.classChooseShow(userBean,
				pageInfoBean);

		request.setAttribute("userCourseListBean", userCourseListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		return "Success";
	}

	// 选课列表_查询
	public String classChooseQuery() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// pageInfo
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseName(this.getCourseName());

		// UserCourseListBean
		UserCourseListBean userCourseListBean = new UserCourseListBean();
		userCourseListBean = courseService.classChooseQuery(userBean,
				pageInfoBean,courseBean);

		request.setAttribute("userCourseListBean", userCourseListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		return "Success";
	}

	// 提交选课
	public String classChoose() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));


		int result = courseService.classChoose(userBean, courseIdList);
		if (result == Constants.RESULT_classChooseSuccess) {// 选课成功
			request.setAttribute("message",
					Constants.MESSAGE_SUCCESS_classChooseSuccess);
			return "Success";
		} else if (result == Constants.RESULT_classChooseFail) {// 选课失败
			request.setAttribute("message",
					Constants.MESSAGE_ERROR_classChooseFail);
			return "ChooseFail";
		}
		return "Success";
	}
}
